package cn.wawi.controller.data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import cn.wawi.common.Json;
import cn.wawi.common.easyui.DataGrid;
import cn.wawi.controller.BaseController;
import cn.wawi.entity.data.QuestionBank;
import cn.wawi.entity.data.QuestionOption;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/data_qBank")
public class QuestionBankController extends BaseController<QuestionBank>{
	
	@InitBinder({"questionType","skill","grade"})
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("grade.");
		binder.setFieldDefaultPrefix("skill.");
		binder.setFieldDefaultPrefix("questionType.");
	}
	
	@ResponseBody
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public DataGrid list(HttpServletRequest request){
		return findAllByPage(request);
	}
	
	@ResponseBody
	@RequestMapping(value="/getOption",method=RequestMethod.GET)
	public List<QuestionOption> getOption(long bankId){
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("ques_id", bankId);
		return questionOptionDao.findAll(map);
	}

	@ResponseBody
	@RequestMapping(value="/saveOption",method=RequestMethod.POST)
	public Json saveOption(long bankId,String optionId){
		QuestionBank q=new QuestionBank();
		q.setId(bankId);
		if(optionId==null||"".equals(optionId.trim())){
			QuestionOption qo=new QuestionOption();
			qo.setQuestionBank(q);
			questionOptionDao.update(qo);
		}else{
			for(String s: optionId.split(",")){
				QuestionOption qo=new QuestionOption();
				qo.setQuestionBank(q);
				qo.setId(Long.parseLong(s));
				questionOptionDao.update(qo);
			}
		}
		return new Json("saveOption", "保存!");
	}
}
